package com.atguigu.rabbitmq.springbootmq.contorller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.atguigu.rabbitmq.springbootmq.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("ttl")
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMsg/{message}")
    public void sendMsg(@PathVariable String message){

        log.info("=========当前时间：{}发送一条信息={}给连个ttl队列", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_FORMATTER),message);

        rabbitTemplate.convertAndSend("X","XA","发送到QA"+message);
        rabbitTemplate.convertAndSend("X","XB","发送到QB"+message);
    }

    @GetMapping("sendDelayMsg/{message}/{delayTime}")
    public  void sendMsg(@PathVariable String message,@PathVariable Integer delayTime){
        log.info("=========当前时间：{}发送一条信息={}给连个延时队列", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_FORMATTER),message);

        rabbitTemplate.convertAndSend(DelayedQueueConfig.EXCHANGE_NM,DelayedQueueConfig.ROUTING_KEY,message,correlatingData->{
            correlatingData.getMessageProperties().setDelay(delayTime);
            return  correlatingData;
        });
    }
}
